﻿using Acme.BookStore.Dishes;
using Acme.BookStore.RoomTypes;
using Microsoft.AspNetCore.Mvc;
using SC.UserCenter.Infra;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Acme.BookStore.ClassifyManages
{
    /// <summary>
    ///商品分类接口实现
    /// </summary>
    [ApiExplorerSettings(GroupName = "v2")]
    public class ClassifyManageServices : ApplicationService, IClassifyManageServices
    {
        /// <summary>
        ///注入
        /// </summary>
        private readonly IRepository<ClassifyManage> repository;
        /// <summary>
        ///构造函数
        /// </summary>
        /// <param name="repository"></param>
        public ClassifyManageServices(IRepository<ClassifyManage> repository)
        {
            this.repository = repository;
        }

        /// <summary>
        ///显示分类
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult> GetClassifyManage(ClassifyManageDTO dto)
        {
            var list = await repository.GetListAsync();
            if(!string.IsNullOrWhiteSpace(dto.ClassifyName))
            {
                list = list.Where(x => x.ClassifyName.Contains(dto.ClassifyName)).ToList();
            }
            return ApiResult.Ok(list);
        }

        /// <summary>
        ///添加分类
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("PostClassifyManage")]
        public async Task<ApiResult> PostClassifyManage(ClassifyManageDTO dto)
        {
            try
            {
                try
                {
                    var list = ObjectMapper.Map<ClassifyManageDTO,ClassifyManage>(dto);
                    await repository.InsertAsync(list);
                    return ApiResult.Ok(data: list);
                }
                catch (Exception ex)
                {
                    ex.HelpLink = "操作失败";
                    return ApiResult.Fail(msg: ex.Message);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        ///删除
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpDelete("DeleteClassifyManage")]
        public async Task<ApiResult> DeleteClassifyManage(Guid? Id)
        {
            try
            {
                await repository.DeleteAsync(x => x.Id == Id);
                return ApiResult.Ok();
                
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        ///分类更新
        /// </summary>
        /// <param name="dto">实体</param>
        /// <returns>返回实体</returns>
        [HttpPut("PutClassifyManage")]
        public async Task<ApiResult> PutClassifyManage(ClassifyManageUpdateDTO dto)
        {
            try
            {
                var list = await repository.FirstOrDefaultAsync(x => x.Id == dto.Id);
                if (list == null)
                {
                    return ApiResult.Fail(msg: "分类不能为空");
                }

                ObjectMapper.Map(dto, list);
                await repository.UpdateAsync(list);
                return ApiResult.Ok(data: list);
            }
            catch (Exception ex)
            {
                return ApiResult.Fail(msg: ex.Message);
            }
        }
    }
}
